<%- include("components/header",{type:'login'}) %>

<div id="fh5co-page">
	<div id="fh5co-main">
		<div class="container">
			<form class="form-signin" onsubmit="return false;">
				<label class="sr-only">用户名</label>
				<input type="text" name="name" class="form-control" placeholder="用户名" autofocus>
				<label class="sr-only">密码</label>
				<input type="password" name="password" class="form-control" placeholder="密码">
				<button class="btn btn-lg btn-primary btn-block submit">登录</button>
			</form>
		</div>
	</div>
</div>

<script>
	$('.submit').click(()=>{
		if ($('input[name=name]').val().trim() == '' || $('input[name=password]').val().trim() == '' ) {
			fade('请输入用户名或密码')
		}else{
			$.ajax({
				url: "/login",
				data: {
					name: $('input[name=name]').val(),
					password: $('input[name=password]').val(),
				},
				type: "POST",
				cache: false,
				dataType: 'json',
				success: function (res) {
					if (!res) {
						$('input').val('')
						fade('用户名或密码错误！')
					} else{
						fade('登录成功')
						setTimeout(()=>{
							window.location.href = "/posts"
						},1000)               	
					}
				},
				error: function () {
					fade('用户名或密码错误！')
				}
			})			
		}
	})
</script>
    
<%- include("components/footer") %>